package cscie160.project.bank;
import cscie160.project.atm.ATMException;
/**
 * This class implements a minimal wrapper needed to hold the information
 * that models a bank account.
 * @author Michael Melnikov   
*/
public class Account
{
    /**
    * object representing complex account info
    * account info consists of account number and PIN
    */
    AccountInfo accountInfo;
    
    /**
    * balance on the account
    */
    private int balance;
    
    /**
    * declares the minimal number acceptable as account Id.
    */    
    private final static int minAccountNumber = 1;    

    /**
    * 1-arg constructor creates account with zero balance
    * @param accountInfo AccountInfo     
    */   
    public Account(AccountInfo accountInfo) throws ATMException
    {
        if (accountInfo == null)
            throw new ATMException("account without AccountInfo may not be created");
        if (accountInfo.getAccountNumber() < minAccountNumber)
            throw new ATMException("account with ID < " + minAccountNumber + " may not be created");
        this.accountInfo = accountInfo;
        balance = 0;
    }
    
    /**
    * returns object containing account Id and Pin
    * @return AccountInfo
    */   
    public AccountInfo getAccountInfo()
    {
        return accountInfo;
    }
    
    /**
    * deposits sum equal to its argument
    * @param amount int    
    */
    public void deposit(int amount) throws ATMException
    {
        if (amount < 0)
            throw (new ATMException("negative amount can't be deposited."));    
        balance += amount;
    }
    
    /**
    * withdraws sum equal to its argument.
    * Throws exception if the funds are insufficient.
    * @param amount int
    */    
    public void withdraw(int amount) throws ATMException
    {
        if (amount > balance)
            throw new ATMException(getClass().getName() + ".withdraw: account with ID=" +
                accountInfo.getAccountNumber() + " does not have enough funds.");
        if (amount < 0)
            throw (new ATMException(getClass().getName() + ".withdraw: account with ID=" +
                accountInfo.getAccountNumber() + " negative amount can't be withdrawn."));                
        balance -= amount;    
    }    
    
    /**
    * returns balance on the account
    * @return int    
    */
    public int getBalance()
    {
        return balance;    
    }
    
    /**
    * prints human readable information about the account
    * @return String    
    */
    public String toString()
    {
        StringBuffer sb = new StringBuffer("{{accountNumber=");
        sb.append(Integer.toString(accountInfo.getAccountNumber()));
        sb.append(";pin=");
        sb.append(Integer.toString(accountInfo.getPin()));
        sb.append("}; balance=");
        sb.append(Integer.toString(balance));
        sb.append("}");
        return sb.toString();    
    }    
	
	/**
	* returns minimal allowed account number
	* @return int
	*/
	public static int getMinAccountNumber()
	{
		return minAccountNumber;
	}
}

